import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import { Redis as RedisClient } from 'ioredis';
import { s } from './dataString';

@Injectable()
export class InitService implements OnModuleInit {
  constructor(@InjectRedis() private readonly redis: RedisClient) {}

  async onModuleInit() {
    const wallets = this.getInitialWallets();
    const pipeline = this.redis.pipeline();

    wallets.forEach((wallet) => {
      const key = wallet.address;
      wallet.data.forEach((value, index) => {
        pipeline.hset(key, `data:${index}`, JSON.stringify(value));
      });
    });

    await pipeline.exec();
    console.log('Initial wallets added to Redis');
  }

  private getInitialWallets(): { address: string; data: any }[] {
    return Object.entries(s).map(([key, value]) => {
      return {
        address: key,
        data: value,
      };
    });
  }
}
